package com.linn.home.controller;

import com.linn.frame.controller.BaseController;
import com.linn.frame.entity.ResultBean;
import com.linn.frame.shiro.realm.ShiroUser;
import com.linn.frame.util.SysContent;
import com.linn.home.entity.Menu;
import com.linn.home.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 菜单controller
 * Created by Administrator on 2018/8/4.
 */
@Controller
public class MenuController extends BaseController {

    @Resource
    private MenuService menuService;

    /**
     * 跳转到菜单列表
     * @return
     */
    @RequestMapping("/admin/toMenuList")
    private String toMenuList() {
        return "admin/menuList";
    }

    /**
     * 获取菜单列表数据
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/getMenuData")
    private List<Menu> getMenuData(){
        List<Menu> menuList = menuService.findMenuList();
        return menuList;
    }

    /**
     * 跳转到添加菜单form
     * @param pId
     * @return
     */
    @RequestMapping("/admin/toAddMenuForm/{pId}")
    private String toAddMenuForm(@PathVariable("pId") int pId, Model model){
        Menu pMenu = new Menu();
        if(pId == 0){
            pMenu.setMenuId(0);
            pMenu.setName("根目录");
        }else{
            pMenu = menuService.findMenuByid(pId);
        }
        model.addAttribute("pMenu",pMenu);
        return "admin/menuForm";
    }

    /**
     * 跳转到更新菜单form
     * @param id
     * @return
     */
    @RequestMapping("/admin/toUpdateMenuForm/{id}")
    private String toUpdateMenuForm(@PathVariable("id") Integer id, Model model){
        Menu menu = menuService.findMenuByid(id);
        Menu pMenu = new Menu();
        if(menu.getParentId() == 0){
            pMenu.setMenuId(0);
            pMenu.setName("根目录");
        }else {
            pMenu = menuService.findMenuByid(menu.getParentId());
        }
        model.addAttribute("pMenu",pMenu);
        model.addAttribute("menu",menu);
        return "admin/menuForm";
    }

    /**
     * 添加/更新菜单
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("admin/addOrUpdateMenu")
    private ResultBean addOrUpdateMenu(Menu menu){
        if (StringUtils.isEmpty(menu.getMenuId())) {
            //添加
            menu.setGmtCreate(new Date());
            menu.setGmtModified(new Date());
            int ret = menuService.addMenu(menu);

        } else {
            //更新
            menu.setGmtModified(new Date());
            int ret = menuService.updateMenu(menu);
        }
        return new ResultBean(SysContent.SUCCESS, "成功");
    }

    /**
     * 删除菜单
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("admin/delMenu")
    private ResultBean delMenu(Menu menu){
        if (StringUtils.isEmpty(menu.getMenuId())) {
            return new ResultBean(SysContent.ERROR, "id不能为空");
        } else {
            int count = menuService.findCountByPid(menu.getParentId());
            if(count>0){
                return new ResultBean(SysContent.ERROR, "存在子菜单，不能删除");
            }
            int ret = menuService.deleteMenu(menu);
        }
        return new ResultBean(SysContent.SUCCESS, "成功");
    }
}
